﻿#include "RotationLabel.h"
#include <QPainter>
#include <QDebug>

namespace GeneralUI {

	RotationLabel::RotationLabel(QWidget *parent)
		:QLabel(parent)
		, m_timer(new QTimer(this))
		, m_icon(QPixmap())
		, m_rotateIndex(0)
		, m_angle(30)
		, m_rotateAngle(0)
	{
		connect(m_timer, &QTimer::timeout, this, &RotationLabel::slotIconRotate);
	}

	RotationLabel::~RotationLabel()
	{

	}

	void RotationLabel::setIcon(QPixmap &pixMap)
	{
		m_icon = pixMap.copy();
	}

	void RotationLabel::setAngle(double angle)
	{
		m_angle = angle;
	}

	void RotationLabel::start(int interval)
	{
		m_timer->start(interval);
	}

	void RotationLabel::stop()
	{
		m_timer->stop();
	}

	void RotationLabel::slotIconRotate()
	{
		m_rotateIndex++;
		m_rotateAngle = fmod(m_angle * m_rotateIndex, 360);
		update();
	}

	void RotationLabel::hideEvent(QHideEvent * event)
	{
		__super::hideEvent(event);
	}

	void RotationLabel::paintEvent(QPaintEvent *event)
	{
		QPainter painter(this);
		painter.translate(this->width() / 2, this->height() / 2);
		painter.setRenderHint(QPainter::SmoothPixmapTransform);
		painter.setRenderHint(QPainter::Antialiasing);
		painter.rotate(m_rotateAngle);
		int width = m_icon.width();
		int height = m_icon.height();
		painter.drawPixmap(QRect(-width / 2, -height / 2, width, height), m_icon);
		__super::paintEvent(event);
	}

}//namespace OfUI 